package com.three.storemanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.Store;
import com.three.core.entity.User;
import com.three.core.vo.R;
import com.three.storemanage.feign.UserFeign;
import com.three.storemanage.mapper.StoreMapper;
import com.three.storemanage.service.StoreService;
import com.three.storemanage.vo.SearchForm;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @FileName: StoreServiceImpl
 * @Description:
 * @Author: GYX
 * @CreateTime: 2025/3/13 15:36
 * @Version: 1.0.0
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StoreMapper storeMapper;
    @Override
    public R<Page<Store>> findStoreByCondition(SearchForm searchForm) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        R<User> feign = userFeign.getByName((String) userName);
        User user = feign.getData();
        Integer userId = user.getUserId();
        Page<Store> page = new Page<>(searchForm.getCurrentPage(),searchForm.getPageSize());
        Page<Store> records = storeMapper.findStoreByCondition(page,searchForm.getStoreName(),userId);
        return new R<>(200,"查询成功",records);
    }

    @Override
    public R deleteStoreById(Integer storeId) {
        int i = storeMapper.deleteById(storeId);
        if (i>0){
            return new R(200,"删除成功",null);
        }else {
            return new R(500,"删除失败",null);
        }
    }

    @Override
    public R updateStore(Store store) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        store.setUpdateBy((String) userName);
        store.setUpdateTime(new Date());
        int i = storeMapper.updateByPrimaryKeySelective(store);
        if (i>0){
            return new R(200,"修改成功",null);
        }else {
            return new R(500,"修改失败",null);
        }
    }

    @Override
    public R insertStore(Store store) {
        String token = request.getHeader("token");
        Object userName = redisTemplate.opsForHash().get("user::login::" + token, "name");
        store.setCreateBy((String) userName);
        store.setCreateTime(new Date());
        int i = storeMapper.insertSelective(store);
        if (i>0){
            return new R(200,"添加成功",null);
        }else {
            return new R(500,"添加失败",null);
        }
    }

    @Override
    public R<Store> selectStoreByStoreId(Integer storeId) {
        Store store = storeMapper.selectByPrimaryKey(Long.valueOf(storeId));
        if (store != null){
            return new R<>(200,"查询成功",store);
        }else {
            return new R<>(500,"查询失败",null);
        }
    }

    @Override
    public R<List<Store>> selectAllStore() {
        List<Store> stores = storeMapper.selectAll();
        if (stores != null && !stores.isEmpty()){
            return new R<>(200,"查询成功",stores);
        }else {
            return new R<>(500,"查询失败",null);
        }
    }

    @Override
    public R<List<Store>> selectStoreByUserId(Integer userId) {
        List<Store> stores = storeMapper.selectStoreBindByUserId(userId);
        if (stores != null && !stores.isEmpty()){
            return new R<>(200,"查询成功",stores);
        }else {
            return new R<>(500,"查询失败",null);
        }
    }

    @Override
    public R<List<Store>> selectStoreByStoreName(String storeName) {
        List<Store> stores = storeMapper.selectStoreByStoreName(storeName);
        if (stores != null && !stores.isEmpty()){
            return new R<>(200,"查询成功",stores);
        }else {
            return new R<>(500,"查询失败",null);
        }
    }


}
